package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.oa.JobPlanType;
import com.prs.crm.service.oa.JobPlanTypeService;

public class JobPlanTypeAction extends BaseAction {
	private JobPlanType jobPlanType;
	private JobPlanTypeService jobPlanTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;

	@Action("listJobPlanType")
	public String listJobPlanType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				JobPlanType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getJobPlanTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editJobPlanType")
	public String editJobPlanType() {
		return SUCCESS;
	}

	@Action(value = "saveJobPlanType", results = {
			@Result(name = "success", type = "redirect", location = "listJobPlanType"),
			@Result(name = "error", type = "chain", location = "editJobPlanType") })
	public String saveJobPlanType() {
		if (jobPlanType.getName() == null
				|| "".equals(jobPlanType.getName().trim())) {
			this.addActionError("请输入工作计划类型名");
			return ERROR;
		}
		if (jobPlanType.getType() == null
				|| "".equals(jobPlanType.getType().trim())) {
			addActionError("所属计划不能为空");
			return ERROR;
		}
		this.getJobPlanTypeService().save(jobPlanType);
		return SUCCESS;
	}

	@Action(value = "removeJobPlanType", results = {
			@Result(name = "success", type = "redirect", location = "listJobPlanType"),
			@Result(name = "error", type = "chain", location = "listJobPlanType") })
	public String removeJobPlanType() {
		if (ids == null) {
			this.addActionError("请选择要删除的工作计划类型");
			return ERROR;
		}
		for (Integer id : ids) {
			JobPlanType type = this.getJobPlanTypeService().get(id);
			if (type.getJobPlanDetails().size() != 0) {
				this.addActionError("已有工作计划引用,不能删除");
				return ERROR;
			}
		}
		this.getJobPlanTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public JobPlanType getJobPlanType() {
		return jobPlanType;
	}

	public void setJobPlanType(JobPlanType jobPlanType) {
		this.jobPlanType = jobPlanType;
	}

	public JobPlanTypeService getJobPlanTypeService() {
		return jobPlanTypeService;
	}

	public void setJobPlanTypeService(JobPlanTypeService jobPlanTypeService) {
		this.jobPlanTypeService = jobPlanTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

}
